package com.czn.controller.admin;

import com.czn.entity.Patient;
import com.czn.service.IPatientService;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员对患者管理的控制器
 *
 * @author ChenZeNan
 * @create 2021-04-17 15:29
 */
@Controller
@RequestMapping("/admin/patient")
public class AdminPatientController {

    @Autowired
    private IPatientService patientService;

    /**
     * 跳转到患者管理页面
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @param certId
     * @param model
     * @return
     */
    @RequestMapping("/manage")
    public String patientManage(@RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                @RequestParam(required = false) String name,
                                @RequestParam(required = false) String certId,
                                Model model) {
        PageQuery<Patient> page = patientService.findPatientList(pageNo, pageSize, name, certId);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("name", name);
        model.addAttribute("certId", certId);
        model.addAttribute("path", "/admin/patient/manage");
        model.addAttribute("params", "");
        if (name != null) {
            model.addAttribute("params", "&name=" + name);
        }
        return "admin/patientManage";
    }

    /**
     * 跳转到新增患者页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String toAddFormPage(Model model) {
        model.addAttribute("patient", new Patient());
        return "admin/patientForm";
    }

    /**
     * 新增患者
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody Patient patient) {
        ResponseResult result = patientService.addPatient(patient);
        return result;
    }

    /**
     * 跳转到患者修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String toEditFormPage(@PathVariable Integer id, Model model) {
        model.addAttribute("patient", patientService.findPatientById(id));
        return "admin/patientForm";
    }

    /**
     * 编辑患者
     *
     * @param patient
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseResult edit(@RequestBody Patient patient) {
        ResponseResult result = patientService.editPatient(patient);
        return result;
    }

    /**
     * 删除患者
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseResult delete(@PathVariable Integer id) {
        ResponseResult result = patientService.deletePatientById(id);
        return result;
    }
}
